package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.io.api.CommunityApiIO;
import com.drinks.yiniang.ui.activity.community.ReportUserActivity;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.dialog.BaseDialogFragment;
import com.hansen.library.ui.widget.view.LineView;
import com.hansen.library.utils.ClipboardUtils;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 退款原因dialog
 *
 * @author han
 * @date 2019/11/12
 */
public class ShareDialog extends BaseDialogFragment implements View.OnClickListener {

    public static final int SHARE_WX_FRIENDS = 0x01;
    public static final int SHARE_WX_MOMENTS = 0x02;
    public static final int SHARE_QQ = 0x03;
    public static final int SHARE_WEIBO = 0x04;
    public static final int SHARE_COPY_LINK = 0x05;
    public static final int SHARE_REPOR = 0x06;
    public static final int SHARE_BLOCK = 0x07;

    //标题
    private TextView tv_dialog_share_title;
    //微信
    private TextView tv_dialog_share_wx;
    //朋友圈
    private TextView tv_dialog_share_moment;
    //微博
    private TextView tv_dialog_share_weibo;
    //复制链接
    private TextView tv_dialog_share_link;
    //举报
    private TextView tv_dialog_share_report;
    //拉黑
    private TextView tv_dialog_share_block;
    private LineView ll_dialog_share_line;
    //关闭
    private Button btn_share_close;

    private OnShareActionListener onShareActionListener;

    //显示分享
    private boolean showShare = true;
    //显示举报拉黑
    private boolean showReportBlock;

    //相关用户id
    private String mUserId;
    //社区文章id
    private String mCommunityId;
    //链接
    private String mLinkUrl;

    public static ShareDialog newInstance() {

        return new ShareDialog();
    }

    public static ShareDialog newInstance(String userId, String communityId, boolean showShare, boolean showReportBlock) {

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyUserId, userId);
        bundle.putString(Constants.KeyCommunityId, communityId);
        bundle.putBoolean(Constants.KeyShow, showShare);
        bundle.putBoolean(Constants.KeyShowReportBlock, showReportBlock);

        ShareDialog dialog = newInstance();
        dialog.setArguments(bundle);

        return dialog;
    }

    public static ShareDialog newInstance(String userId, String communityId, String linkUrl, boolean showShare, boolean showReportBlock) {

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyUserId, userId);
        bundle.putString(Constants.KeyCommunityId, communityId);
        bundle.putBoolean(Constants.KeyShow, showShare);
        bundle.putBoolean(Constants.KeyShowReportBlock, showReportBlock);
        bundle.putString(Constants.KeyUrl, linkUrl);

        ShareDialog dialog = newInstance();
        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(getParentFragment() != null) {
                onShareActionListener = (OnShareActionListener) getParentFragment();
            } else {
                onShareActionListener = (OnShareActionListener) context;
            }
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + "must implement OnShareActionListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 定义Dialog布局和参数
        mDialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);

        // 获取Dialog布局
        View mView = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_share, null);

        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        mDialog.setContentView(mView);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        tv_dialog_share_title = mView.findViewById(R.id.tv_dialog_share_title);
        tv_dialog_share_wx = mView.findViewById(R.id.tv_dialog_share_wx);
        tv_dialog_share_moment = mView.findViewById(R.id.tv_dialog_share_moment);
        tv_dialog_share_weibo = mView.findViewById(R.id.tv_dialog_share_weibo);
        tv_dialog_share_link = mView.findViewById(R.id.tv_dialog_share_link);
        tv_dialog_share_report = mView.findViewById(R.id.tv_dialog_share_report);
        tv_dialog_share_block = mView.findViewById(R.id.tv_dialog_share_block);

        ll_dialog_share_line = mView.findViewById(R.id.ll_dialog_share_line);

        btn_share_close = mView.findViewById(R.id.btn_share_close);

        tv_dialog_share_wx.setOnClickListener(this);
        tv_dialog_share_moment.setOnClickListener(this);
        tv_dialog_share_weibo.setOnClickListener(this);
        tv_dialog_share_link.setOnClickListener(this);
        tv_dialog_share_report.setOnClickListener(this);
        tv_dialog_share_block.setOnClickListener(this);
        btn_share_close.setOnClickListener(this);

        initData();

        return mDialog;
    }

    private void initData() {
        mContext = getActivity();

        if (getArguments() != null) {
            mUserId = getArguments().getString(Constants.KeyUserId);
            mCommunityId = getArguments().getString(Constants.KeyCommunityId);
            showShare = getArguments().getBoolean(Constants.KeyShow, true);
            showReportBlock = getArguments().getBoolean(Constants.KeyShowReportBlock, false);
            mLinkUrl = getArguments().getString(Constants.KeyUrl);
        }

        if (showShare) {
            tv_dialog_share_title.setText(R.string.text_share);

            tv_dialog_share_wx.setVisibility(View.VISIBLE);
            tv_dialog_share_moment.setVisibility(View.VISIBLE);
            //tv_dialog_share_weibo.setVisibility(View.VISIBLE);
            tv_dialog_share_link.setVisibility(View.VISIBLE);
        } else {
            tv_dialog_share_title.setText("");

            tv_dialog_share_wx.setVisibility(View.GONE);
            tv_dialog_share_moment.setVisibility(View.GONE);
            //tv_dialog_share_weibo.setVisibility(View.GONE);
            tv_dialog_share_link.setVisibility(View.GONE);
        }

        if (showReportBlock) {
            tv_dialog_share_report.setVisibility(View.VISIBLE);
            tv_dialog_share_block.setVisibility(View.VISIBLE);
        } else {
            tv_dialog_share_report.setVisibility(View.GONE);
            tv_dialog_share_block.setVisibility(View.GONE);
        }

        tv_dialog_share_link.setVisibility(StringUtils.isEmpty(mLinkUrl) ? View.GONE : View.VISIBLE);
        ll_dialog_share_line.setVisibility(showShare && showReportBlock ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        dismiss();

        switch (v.getId()) {
            case R.id.tv_dialog_share_wx://微信
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_WX_FRIENDS);
                }
                break;
            case R.id.tv_dialog_share_moment://朋友圈
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_WX_MOMENTS);
                }
                break;
            case R.id.tv_dialog_share_weibo://微博
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_WEIBO);
                }
                break;
            case R.id.tv_dialog_share_link://复制链接
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_COPY_LINK);
                }
                if (StringUtils.isEmpty(mLinkUrl)) {
                    return;
                }
                ClipboardUtils.copyTextToClipboard(getActivity(), mLinkUrl);

                ToastUtils.showShort(R.string.text_copy_success);
                break;
            case R.id.tv_dialog_share_report://举报
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_REPOR);
                }
                Intent intent = new Intent(mContext, ReportUserActivity.class);
                intent.putExtra(Constants.KeyCommunityId, mCommunityId);
                startActivity(intent);
                break;
            case R.id.tv_dialog_share_block://拉黑
                if(onShareActionListener != null) {
                    onShareActionListener.onShare(SHARE_BLOCK);
                }
                doBlackUser();
                break;
            case R.id.btn_share_close://取消
                break;
            default:
                break;
        }
    }

    //拉黑用户
    private void doBlackUser() {
        if (StringUtils.isEmpty(mUserId)) {
            ToastUtils.showShort(R.string.text_user_not_exist);
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().doCommunityBlack(mUserId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                dismiss();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    public interface OnShareActionListener {
        void onShare(int shareType);
    }

    @Override
    public void onDestroy() {
        mDialog = null;
        super.onDestroy();
    }
}
